﻿Public Class CalculatorForm
	Private partialSum As IList(Of Integer)
	Private Const SumOperator As Char = "+"

	Protected Overrides Sub OnLoad(e As System.EventArgs)
		partialSum = New List(Of Integer)
		partialSumListBox.DataSource = partialSum
	End Sub

	Private Sub OperandTextBoxChanged(sender As System.Object, e As System.EventArgs) Handles operandTextBox.TextChanged
		Dim text As String = operandTextBox.Text

		If (text.EndsWith(SumOperator)) Then
			Dim value = Integer.Parse(text.TrimEnd(SumOperator))
			partialSum.Add(value)
			operandTextBox.Clear()

			partialSumListBox.DataSource = Nothing
			partialSumListBox.DataSource = partialSum
		End If
	End Sub

	Private Sub OperandTextBoxKeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles operandTextBox.KeyUp
		If (e.KeyCode <> Keys.Enter) Then
			Return
		End If

		resultTextBox.Text = partialSum.Sum()
	End Sub

	Private Sub PartialSumListBoxSelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles partialSumListBox.SelectedIndexChanged
		resultTextBox.Text =
		 partialSum.Take(partialSumListBox.SelectedIndex + 1).Sum()
	End Sub
End Class
